package com.rzico.basics.amap.util;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.rzico.basics.amap.pojo.GeoPOIQuery;
import com.rzico.basics.amap.pojo.query.Poi;
import com.rzico.basics.amap.pojo.request.POIRequest;

import java.util.ArrayList;
import java.util.List;

/**
 * 搜索POI
 */
public class GeoQueryUtil {

    //关键字搜索API服务地址
    private static String map_keywords_url = "https://restapi.amap.com/v3/place/text?parameters";
    //周边搜索API服务地址
    private static String map_around_url = "https://restapi.amap.com/v3/place/around?parameters";
    //多边形搜索API服务地址
    private static String map_polygon_url = "https://restapi.amap.com/v3/place/polygon?parameters";
    //ID查询搜索API服务地址
    private static String map_detail_url = "https://restapi.amap.com/v3/place/detail?parameters";

    /**
     * 关键词搜索
     */
    public static GeoPOIQuery keywordsPlace(POIRequest request) {
        GeoPOIQuery geoPOIQuery = null;

        if (request == null) {
            geoPOIQuery = new GeoPOIQuery();
            geoPOIQuery.setError(true);
            geoPOIQuery.setInfo("输入的参数为空");
            return geoPOIQuery;
        }

        if (request.getKey() == null || "".equals(request.getKey())) {
            geoPOIQuery = new GeoPOIQuery();
            geoPOIQuery.setError(true);
            geoPOIQuery.setInfo("请求的服务标识权限为空");
            return geoPOIQuery;
        }

        if (request.getKeywords() == null || "".equals(request.getKeywords())) {
            geoPOIQuery = new GeoPOIQuery();
            geoPOIQuery.setError(true);
            geoPOIQuery.setInfo("查询关键字为空");
            return geoPOIQuery;
        }

        try {
            String result = OKHttpUtil.httpGet(map_keywords_url.replace("parameters", "") + request.keywordsToString());
            JSONObject jsonObject = JSONObject.parseObject(result);
            geoPOIQuery = new GeoPOIQuery();
            // 解析json
            JSONArray jsonArray = jsonObject.getJSONArray("pois");
            List<Poi> pois = new ArrayList<>();
            // 遍历解析出来的结果
            if ((jsonArray != null) && (jsonArray.size() > 0)) {
                for (int i=0;i<jsonArray.size();i++) {
                    JSONObject po = jsonArray.getJSONObject(i);
                    Poi poi = new Poi();
                    poi.setAddress(po.getString("address"));
                    poi.setId(po.getString("id"));
                    poi.setLocation(po.getString("location"));
                    poi.setName(po.getString("name"));
                    poi.setType(po.getString("type"));
                    poi.setAdCode(po.getString("adcode"));
                    poi.setAdName(po.getString("pname").concat(po.getString("cityname")).concat(po.getString("adname")));
                    poi.setTypecode(po.getString("typecode"));
                    poi.setCityName(po.getString("cityname"));
                    pois.add(poi);
                }
            }

            geoPOIQuery.setPois(pois);
            geoPOIQuery.setError(false);


        } catch (Exception e) {
            e.printStackTrace();
        }

        return geoPOIQuery;
    }

    /**
     * 周边搜索
     */
    public static GeoPOIQuery aroundPlace(POIRequest request) {
        GeoPOIQuery geoPOIQuery = null;

        if (request == null) {
            geoPOIQuery = new GeoPOIQuery();
            geoPOIQuery.setError(true);
            geoPOIQuery.setInfo("输入的参数为空");
            return geoPOIQuery;
        }

        if (request.getKey() == null || "".equals(request.getKey())) {
            geoPOIQuery = new GeoPOIQuery();
            geoPOIQuery.setError(true);
            geoPOIQuery.setInfo("请求的服务标识权限为空");
            return geoPOIQuery;
        }

        if (request.getLocation() == null || "".equals(request.getLocation())) {
            geoPOIQuery = new GeoPOIQuery();
            geoPOIQuery.setError(true);
            geoPOIQuery.setInfo("查询坐标点为空");
            return geoPOIQuery;
        }

        try {
            String result = OKHttpUtil.httpGet(map_around_url.replace("parameters", "") + request.aroundToString());
            geoPOIQuery = JSONObject.parseObject(result, GeoPOIQuery.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return geoPOIQuery;
    }

    /**
     * 多边形搜索
     */

    public static GeoPOIQuery polygonPlace(POIRequest request) {
        GeoPOIQuery geoPOIQuery = null;

        if (request == null) {
            geoPOIQuery = new GeoPOIQuery();
            geoPOIQuery.setError(true);
            geoPOIQuery.setInfo("输入的参数为空");
            return geoPOIQuery;
        }

        if (request.getKey() == null || "".equals(request.getKey())) {
            geoPOIQuery = new GeoPOIQuery();
            geoPOIQuery.setError(true);
            geoPOIQuery.setInfo("请求的服务标识权限为空");
            return geoPOIQuery;
        }

        if (request.getPolygon() == null || "".equals(request.getPolygon())) {
            geoPOIQuery = new GeoPOIQuery();
            geoPOIQuery.setError(true);
            geoPOIQuery.setInfo("查询的经纬度坐标对为空");
            return geoPOIQuery;
        }

        try {
            String result = OKHttpUtil.httpGet(map_polygon_url.replace("parameters", "") + request.polygonToString());
            JSONObject jsonObject = JSONObject.parseObject(result);
            geoPOIQuery = new GeoPOIQuery();
            // 解析json
            JSONArray jsonArray = jsonObject.getJSONArray("pois");
            List<Poi> pois = new ArrayList<>();
            // 遍历解析出来的结果
            if ((jsonArray != null) && (jsonArray.size() > 0)) {
                for (int i=0;i<jsonArray.size();i++) {
                    JSONObject po = jsonArray.getJSONObject(i);
                    Poi poi = new Poi();
                    poi.setAddress(po.getString("address"));
                    poi.setId(po.getString("id"));
                    poi.setLocation(po.getString("location"));
                    poi.setName(po.getString("name"));
                    poi.setType(po.getString("type"));
                    pois.add(poi);
                }
            }

            geoPOIQuery.setPois(pois);
            geoPOIQuery.setError(false);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return geoPOIQuery;

    }

    /**
     * ID搜索
     */
    public static GeoPOIQuery detailPlace(POIRequest request) {
        GeoPOIQuery geoPOIQuery = null;

        if (request == null) {
            geoPOIQuery = new GeoPOIQuery();
            geoPOIQuery.setError(true);
            geoPOIQuery.setInfo("输入的参数为空");
            return geoPOIQuery;
        }

        if (request.getKey() == null || "".equals(request.getKey())) {
            geoPOIQuery = new GeoPOIQuery();
            geoPOIQuery.setError(true);
            geoPOIQuery.setInfo("请求的服务标识权限为空");
            return geoPOIQuery;
        }

        if (request.getId() == null || "".equals(request.getId())) {
            geoPOIQuery = new GeoPOIQuery();
            geoPOIQuery.setError(true);
            geoPOIQuery.setInfo("查询的兴趣ID为空");
            return geoPOIQuery;
        }

        try {
            String result = OKHttpUtil.httpGet(map_detail_url.replace("parameters", "") + request.detailToString());
            geoPOIQuery = JSONObject.parseObject(result, GeoPOIQuery.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return geoPOIQuery;
    }

//    public static void main(String[] args) {
//        POIRequest request = new POIRequest();
//        request.setKey("32239d024f2492896d0b2a2a976da365");
//        request.setKeywords("北京大学");
//        request.setLocation("116.473168,39.993015");
//        request.setPolygon("116.460988,40.006919|116.48231,40.007381;116.47516,39.99713|116.472596,39.985227|116.45669,39.984989|116.460988,40.006919");
//        request.setId("B0FFFAB6J2");
//
//        GeoPOIQuery keywords = GeoQueryUtil.keywordsPlace(request);
//        System.out.println("keywords : " + keywords.getStatus());
//
//        GeoPOIQuery around = GeoQueryUtil.aroundPlace(request);
//        System.out.println("around : " + around.getStatus());
//
//        GeoPOIQuery polygon = GeoQueryUtil.polygonPlace(request);
//        System.out.println("polygon : " + polygon.getStatus());
//
//        GeoPOIQuery detail = GeoQueryUtil.detailPlace(request);
//        System.out.println("detail : " + detail.getStatus());
//
//
//    }
}
